﻿using FineUI;
using System;
using System.Data;
using System.Linq;
using System.Text;
using ZHDJ.BranchCenter.BLL;
using ZHDJ.Core;
using ZHDJ.Core.Util;
using ZHDJ.Data.BLL;
using ZHDJ.Data.Models.Center;
using ZHDJ.WebPage;

namespace ZHDJ.BranchCenter.Module.MeetingLecture
{
    public partial class MeetingLectureEdit : PageBase
    {
        #region ViewPower

        /// <summary>
        /// 本页面的浏览权限，空字符串表示本页面不受权限控制
        /// </summary>
        public override string ViewPower
        {
            get
            {
                return "";
            }
        }
        #endregion

        #region 页面属性
        public string Category
        {
            get
            {
                return this.Request["Category"] ?? "";
            }
        }
        #endregion

        #region Page_Load

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadData();
            }
        }

        private void LoadData()
        {
            btnClose.OnClientClick = ActiveWindow.GetHideReference();
            string id = GetQueryIntValue("id");
            if (id != "")
            {
                PartyMeetingLecture meeting = DB.PartyMeetingLectures.Find(id);
                if (meeting == null)
                {
                    // 参数错误，首先弹出Alert对话框然后关闭弹出窗口
                    Alert.Show("参数错误！", String.Empty, ActiveWindow.GetHideReference());
                    return;
                }

                if (meeting.Category == "Class")
                {
                    txtTitle.Label = "党课标题";
                    ddlMeetType.Hidden = true;
                    txtAddress.Label = "上课地点";
                    cbIsCopy.Hidden = true;
                }
                else
                {
                    txtTitle.Label = "会议标题";
                    txtAddress.Label = "会议地点";
                }

                txtTitle.Text = meeting.Title;
                dpBeginTime.Text = Convert.ToDateTime(meeting.BeginTime).ToString("yyyy-MM-dd");
                dpEndTime.Text = Convert.ToDateTime(meeting.EndTime).ToString("yyyy-MM-dd");
                txtAddress.Text = meeting.Address;
                txtBranchName.Text = ControlBusiness.GetBranchPickerText(meeting.BranchID);

                if (meeting.Category.Contains("Meeting"))
                {
                    ddlMeetType.SelectedValue = meeting.MeetType;

                    if (meeting.MeetType != "0")
                    {
                        cbIsCopy.Enabled = false;
                    }
                    else
                    {
                        cbIsCopy.Checked = meeting.Category == "Class+Meeting";
                    }
                }

                IQueryable<PartyMeetingLectureMember> q = DB.PartyMeetingLectureMembers.Where(t => t.MeetingLectureID == id);

                if (q.Count() > 0)
                {
                    StringBuilder sbLoginID = new StringBuilder();
                    StringBuilder sbName = new StringBuilder();

                    foreach (PartyMeetingLectureMember item in q)
                    {
                        sbLoginID.Append(item.LoginID + ",");
                        sbName.Append(ControlBusiness.GetMembersPickerText(item.LoginID) + ",");
                    }

                    ppMember.Value = sbLoginID.ToString().TrimEnd(',');
                    ppMember.Text = sbName.ToString().TrimEnd(',');
                }

                if (meeting.Status == 4)
                {
                    btnSaveClose.Enabled = btnPublish.Enabled = false;
                }
            }
            else
            {
                if (this.Category == "Class")
                {
                    txtTitle.Label = "党课标题";
                    txtAddress.Label = "上课地点";
                }
                else
                {
                    txtTitle.Label = "会议标题";
                    txtAddress.Label = "会议地点";
                }
                txtBranchName.Text = ControlBusiness.GetBranchPickerText(this.BranchID);
            }
        }
        #endregion

        #region Events

        private void SaveData(bool isPublish)
        {
            string id = GetQueryIntValue("id");
            string _id = id;
            PartyMeetingLecture meeting = null;

            if (id != "")
            {
                //更新
                meeting = DB.PartyMeetingLectures.Find(id);
                if (meeting == null)
                {
                    Alert.Show("参数错误！", String.Empty, ActiveWindow.GetHideReference());
                    return;
                }

                meeting.Title = txtTitle.Text.Trim();
                meeting.Status = isPublish ? 2 : 1;
                meeting.BeginTime = Convert.ToDateTime(dpBeginTime.Text);
                meeting.EndTime = Convert.ToDateTime(dpEndTime.Text);
                meeting.Address = txtAddress.Text.Trim();
                if (cbIsCopy.Checked)
                {
                    meeting.Category = "Class+Meeting";
                }
                else
                {
                    meeting.Category = this.Category;
                }
                if (meeting.Category.Contains("Meeting"))
                {
                    meeting.MeetType = ddlMeetType.SelectedValue;
                }
                meeting.ModifyTime = DateTime.Now;
                meeting.ModifyBy = CurrentUser.LoginID;

                DB.SaveChanges();
            }
            else
            {
                meeting = new PartyMeetingLecture();
                meeting.ID = StringHelper.NewID;
                _id = meeting.ID;
                meeting.Title = txtTitle.Text.Trim();
                meeting.BranchID = this.BranchID;
                meeting.BeginTime = Convert.ToDateTime(dpBeginTime.Text);
                meeting.EndTime = Convert.ToDateTime(dpEndTime.Text);
                meeting.Status = isPublish ? 2 : 1;
                meeting.Address = txtAddress.Text.Trim();
                meeting.IsEnd = false;
                if (cbIsCopy.Checked)
                {
                    meeting.Category = "Class+Meeting";
                }
                else
                {
                    meeting.Category = this.Category;
                }
                if (meeting.Category.Contains("Meeting"))
                {
                    meeting.MeetType = ddlMeetType.SelectedValue;
                }
                meeting.CreateTime = DateTime.Now;
                meeting.ModifyTime = DateTime.Now;
                meeting.CreateBy = CurrentUser.LoginID;
                meeting.ModifyBy = CurrentUser.LoginID;

                DB.PartyMeetingLectures.Add(meeting);

                DB.SaveChanges();
            }

            DB.PartyMeetingLectureMembers.Where(t => t.MeetingLectureID == _id).ToList().ForEach(t => DB.PartyMeetingLectureMembers.Remove(t));

            if (!string.IsNullOrEmpty(ppMember.Value))
            {
                PartyMeetingLectureMember member = null;
                foreach (string item in ppMember.Value.Split(','))
                {
                    member = new PartyMeetingLectureMember();
                    member.ID = StringHelper.NewID;
                    member.MeetingLectureID = _id;
                    member.LoginID = item;
                    member.Status = 1;

                    DB.PartyMeetingLectureMembers.Add(member);

                    DB.SaveChanges();
                }
            }

            //如果创建活动，则发起待办
            if (meeting.Status == 2)
            {
                PartyTaskHelper.AddTaskToMember(_id, txtTitle.Text, SysEnum.TaskCategory.MeetingLecture,
                this.CurrentUser.LoginID, ppMember.Value.Split(','),
                "../../../BranchCenter/Module/MeetingLecture/ApprovalMeetingLectureEdit.aspx?RelationID=" + _id);
            }
        }

        /// <summary>
        /// 保存并关闭按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnSaveClose_Click(object sender, EventArgs e)
        {
            SaveData(false);

            PageContext.RegisterStartupScript(ActiveWindow.GetHidePostBackReference());
        }

        /// <summary>
        /// 发布按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnPublish_Click(object sender, EventArgs e)
        {
            SaveData(true);

            PageContext.RegisterStartupScript(ActiveWindow.GetHidePostBackReference());
        }

        protected void ddlMeetType_SelectedIndexChanged(object sender, EventArgs e)
        {
            cbIsCopy.Enabled = ddlMeetType.SelectedValue == "0";
        }
        #endregion
    }
}